from io import BytesIO

from flask import Blueprint, session, make_response, request, redirect, url_for, flash
from flask import render_template
from APP.utils.identufy_code_util import generate_image

vrf_code_bp = Blueprint(
    'vrf_code_bp',
    __name__,
    url_prefix='/vrfcode'
)

@vrf_code_bp.route('/')
def index():
    return '<h1>Verify Code<h1/>'

@vrf_code_bp.route('/getcode')
def get_code():
    code,image = generate_image()
    out = BytesIO()
    image.save(out,'jpeg')
    out.seek(0)
    resp = make_response(out.read())
    resp.content_type = 'image/jpeg'
    session['code']=code
    print(code)
    return resp

@vrf_code_bp.route('/vrifycode',methods=['POST'])
def verify_code():

    print(request.form['code'],session['code'])
    if request.form['code']==session['code']:
        return redirect('/')
    else:
        return render_template('is_spider.html')
